<?php
/**
 * 报告模块逻辑层
 */
namespace AuctionBack\Logic;

use AuctionBack\Model\CarReportModel;
use AuctionCore\System\AuctionException;

class CarReportLogic
{

    Protected $autoCheckFields = false;

    public $car_id = null;

    /**
     * 构造
     *
     * @param  $car_id or $car_no
     *
     */
    public function __construct($car_no_or_id)
    {
        if (is_numeric($car_no_or_id)) {
            $this->car_id = $car_no_or_id;
        } else {
            $r = D('CarReport', 'Model')->field('car_id')->where("car_no='{$car_no_or_id}'")->find();
            if ($r && count($r) > 0) {
                $this->car_id = $r['car_id'];
            } else {
                AuctionException::make_throw('600002');
            }
        }
    }

    /**
     * 报告详情
     *
     * @return array
     */
    public function detail()
    {
        return D('CarReport', 'Model')->where("car_id='{$this->car_id}'")->find();
    }

    /**
     * 通过车牌号查询车辆
     * @param $data
     * @return mixed
     */
    public static function getCarByPlateNo($data)
    {
        $map = [
            'plate_prefix' => $data['prefix'],
            'plate_suffix' => $data['suffix'],
        ];

        return D('CarReport', 'Model')->where($map)->find();
    }

    /**
     * 通过条件查询报告信息(多条)
     * @param $data
     * @return array
     */
    public static function getCarInfos($data)
    {

        return D('CarReport', 'Model')->field('sid')->where($data)->select();
    }

    /**
     * 通过条件查询报告信息(单条)
     *
     * @param $data
     * @return array
     */
    public static function getCarInfo($data)
    {

        return D('CarReport', 'Model')->where($data)->find();
    }

    /**
     * 根据车辆编号，更新车辆报告信息
     *
     * @param $data
     * @return bool
     */
    public function saveByCarId($data)
    {
        $carId = $this->car_id;
        return D('CarReport', 'Model')->where(['car_id' => $carId])->save($data);
    }

    /**
     * 更新报告
     * @param $data array 更新数据
     * @return Boolean
     * */
    public function update($data)
    {
        $result = D('CarReport', 'Model')->where("car_id='" . $this->car_id . "'")->save($data);
        // echo M()->getLastSql();
        return $result;
    }

    /**
     * 获取车辆所属分公司ID
     * @return mixed
     */
    public function getBranchId()
    {
        $carInfo = $this->detail();
        return D("BranchCity","Model")->where(['city_code'=>$carInfo['location_area']])->getField("branch_id");
    }
}